/*
 * Copyright (C) 2009 M. Hautle.
 * 
 * This file is part of TrackedBean.
 * 
 * TrackedBean is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * TrackedBean is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with TrackedBean. If not, see <http://www.gnu.org/licenses/>.
 */
package ch.trackedbean.binding.action;

import java.util.*;
import javax.swing.*;

/**
 * Base implementation of {@link ISelectionAction} storing the passed selection in a memoryleak avoiding way.
 * 
 * @param <T> The type of the selection
 * 
 * @author M. Hautle
 */
public abstract class AbstractSelectionAction<T> extends AbstractAction implements ISelectionAction<T> {
    /** The current selection. */
    private final SelectionHolder<T> selection = new SelectionHolder<T>();

    /**
     * Default constructor.
     */
    public AbstractSelectionAction() {
    }

    /**
     * Default constructor.
     * 
     * @param name The name of the action
     * @param icon The icon of the action
     */
    public AbstractSelectionAction(String name, Icon icon) {
        super(name, icon);
    }

    /**
     * Default constructor.
     * 
     * @param name The name of the action
     */
    public AbstractSelectionAction(String name) {
        super(name);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getActionID() {
        return getClass().getName();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final boolean selectionChanged(List<T> selection) {
        this.selection.set(selection);
        return showActionForSelection(selection);
    }

    /**
     * Method deciding if this action should be shown/activated for the passed selection.
     * 
     * @param selection The selection (non null)
     * @return True if the action should be shown for the given selection
     */
    protected boolean showActionForSelection(List<T> selection) {
        return true;
    }

    /**
     * Returns the current selection.
     * 
     * @return The selection
     */
    protected List<T> getSelection() {
        return selection.get();
    }
}
